<?php
/** 
* 模型类文件(相册模块)
* 
* 相册模块的数据获取和视图标签的构建
* @author      sinalma  <sinalma@foxmail.com>
* @version     1.0 版本号
*/ 
namespace app\index\model;

use think\Model;
use think\Db;

/** 
 * Class Album
 * 相册模型类 
 *
 * @package app\index\model
 * @author      sinalma  <sinalma@foxmail.com>
 * @version     1.0 版本号
 */ 
class Album extends Model
{

	// 设置当前模型对应的完整数据表名称
    protected $table = 'sinblog_album';
    private $htpURL = '';


    public function pswpPics($pid){
        $pids = $this->getThree($pid);
        $pidstr = "(".join(",",$pids).")";
        $sql = "SELECT * FROM sinblog_album WHERE pic_id IN {$pidstr} ";
        $res = $this->query($sql);
        return $res;
    }
    
    public function getPid($type){
        if ($type=="prev") {
            echo $type;
        }
    }


    /**  
     * 获取指定图片id相邻的两个pic_id,并返回数组包含这3个pic_id
     * 
     * @access public 
     * @param mixed $pid 图片id
     * @return array 
     */
    public function getThree($pid){
        // 默认是倒序
        $sql = "SELECT pic_id from {$this->table} order by time desc";
        $res = $this->query($sql);
        $idx = 0;
        for ($i=0; $i < count($res); $i++) { 
            if($pid == $res[$i]['pic_id']){
                 $idx = $i;
            }
        }
        $pidx = 0;
        $nidx = 0;
        if ($idx == 0){
            $pidx = count($res)-1;
            $nidx = $idx + 1;
        }elseif ($idx == count($res)-1) {
            $pidx = $idx-1;
            $nidx = 0;
        }else{
            $pidx = $idx + 1;
            $nidx = $idx - 1;
        }

        $pids[] = $res[$pidx]['pic_id'];
        $pids[] = $res[$idx]['pic_id'];
        $pids[] = $res[$nidx]['pic_id'];
        return $pids;
    }

    
    private function pids(){

    }


    /**  
     * 获取相册模块数据(页)
     * 
     * @access public 
     * @param mixed $limitStr sql查询的限制,格式:limit 0,5
     * @param mixed $num 每页显示的文章数
     * @return array 数组
     */
    public function getPicsJson($page,$limit){
         // http://ov9qrpbbb.bkt.clouddn.com/sinalmablog/photos/2012/TRIVALZS.jpg
        $photoJson = file_get_contents("{$this->htpURL}/public/static/json/ins.json");
        $photos = json_decode($photoJson,true);
        $photoList = $photos["list"];
        return $photoList;
    }   

    /**  
     * 从数据库中获取相片(全部数据,暂不分页)
     * 以月份为集合
     * 返回关联数组,数组包含所有图片集合(月份),每个集合数组中包含 'date'年月,arr年月集合,包含 year和mouth, pic_id 照片id集合,link 年月下高清图片链接,small_link 缩略图集合,text 图片备注集合,type 图片类型集合,暂只支持图片不支持视频
     * 
     * @access public 
     * @return array
     */
    public function getPics(){
        $curDate = "";
        // 分组数,按月份分
        $group = 0;
        $phots = array();
        $res = $this->query("SELECT * from {$this->table} order by time desc");
        // 构建指定键值对的数据
        for ($i=0;$i<count($res);++$i){
            $date = date('Y-M',$res[$i]['time']);
            $year = date('Y',$res[$i]['time']); 
            $mouth = date('M',$res[$i]['time']); 
            if ($curDate != $date){
                $phots[$group] = array();
                $photos[$group]['date'] = $date; 
            }
            $photos[$group]['arr']['pic_id'][] = $res[$i]['pic_id'];
            $photos[$group]['arr']['year'] = $year;
            $photos[$group]['arr']['mouth'] = $mouth;
            $photos[$group]['arr']['link'][] = $res[$i]['link'];
            $photos[$group]['arr']['small_link'][] = $res[$i]['small_link'];
            $photos[$group]['arr']['text'][] = $res[$i]['note'];
            $photos[$group]['arr']['type'][] = $res[$i]['type'];

            if($curDate != $date){
                $group += 1;
                $curDate = $date;
            }
        }
        return $photos;
    }

}